﻿using SGArchiveSystem7xDB;
using SGArchiveSystem7xDB.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SGArchiveSystem7x.Controllers.User
{
    public class UserController : BaseController
    {
        /// <summary>
        /// 本控制器的名称，用于权限访问 (用户权限)
        /// </summary>
        private string sControllerName = "User";
        // GET: User
        public ActionResult Index()
        {
            var n = this.VaildUser(sControllerName);
            if (n != null)
                return n;

            ///清除必要的SESSION
            App_Code.CSession.Clear_PageSession(Session);

            if(Models.Language._nowLanguage == _VersionLanguage._Chs)
            {
                ViewBag.Text0 = "用户列表";
                ViewBag.Text1 = "姓名:";
                ViewBag.Text2 = "是否启用:";
                List<string[]> list = new List<string[]>();
                list.Add(new string[] { "-1", "全部" });
                list.Add(new string[] { "1", "启用" });
                list.Add(new string[] { "0", "停用" });
                ViewBag.Text3 = list;
                ViewBag.Text4 = "所属角色:";
                ViewBag.Text5 = "查询";
                ViewBag.Text6 = "添加";
                ViewBag.Text7 = "登录名:";
                ViewBag.Text8 = "密码:";
                ViewBag.Text9 = "姓名:";
                ViewBag.Text10 = "IP地址:";
                ViewBag.Text11 = "绑定IP:";
                ViewBag.Text12 = "联系电话:";
                ViewBag.Text13 = "联系邮箱:";
                ViewBag.Text14 = "所属角色:";
                ViewBag.Text15 = "用户状态:";
                ViewBag.Text16 = "保存";
                ViewBag.Text17 = "提示";
                ViewBag.Text18 = "保存成功";
                ViewBag.Text19 = "保存失败";
                ViewBag.Text20 = "编辑";
                ViewBag.Text21 = "重置密码";
                ViewBag.Text22 = "确定要重置密码吗?";
                ViewBag.Text23 = "密码重置成功";
                ViewBag.Text24 = "密码重置失败，请联系管理员";
                ViewBag.Text25 = "已经有相同的登录名";
                ViewBag.Text26 = "添加用户";
                ViewBag.Text27 = "修改用户";
                ViewBag.Text28 = "启用";
                ViewBag.Text29 = "停用";
                ViewBag.Text30 = "是";
                ViewBag.Text31 = "否";
                ViewBag.Text32 = "用户名不能为空";
                ViewBag.Text33 = "姓名不能为空";
            }
            else
            {
                ViewBag.Text0 = "User List";
                ViewBag.Text1 = "Full Name:";
                ViewBag.Text2 = "Status:";
                List<string[]> list = new List<string[]>();
                list.Add(new string[] { "-1", "All" });
                list.Add(new string[] { "1", "Normal" });
                list.Add(new string[] { "0", "No login" });
                ViewBag.Text3 = list;
                ViewBag.Text4 = "Role:";
                ViewBag.Text5 = "Search";
                ViewBag.Text6 = "Add";
                ViewBag.Text7 = "Login Name:";
                ViewBag.Text8 = "Password:";
                ViewBag.Text9 = "Full Name:";
                ViewBag.Text10 = "IP Addr:";
                ViewBag.Text11 = "Lock IP:";
                ViewBag.Text12 = "Phone:";
                ViewBag.Text13 = "Email:";
                ViewBag.Text14 = "Role:";
                ViewBag.Text15 = "Status:";
                ViewBag.Text16 = "Save";
                ViewBag.Text17 = "Tip";
                ViewBag.Text18 = "Save successfully";
                ViewBag.Text19 = "Save failed";
                ViewBag.Text20 = "Edit";
                ViewBag.Text21 = "reset password";
                ViewBag.Text22 = "Are you sure you want to reset your password?";
                ViewBag.Text23 = "Password reset successfully";
                ViewBag.Text24 = "Password reset failed";
                ViewBag.Text25 = "already have the same login name";
                ViewBag.Text26 = "Add User";
                ViewBag.Text27 = "Modify User";
                ViewBag.Text28 = "Normal";
                ViewBag.Text29 = "No login";
                ViewBag.Text30 = "Yes";
                ViewBag.Text31 = "No";
                ViewBag.Text32 = "User name cannot be empty";
                ViewBag.Text33 = "Full name cannot be empty";
            }

            ///角色grid 的列
            var columns = SGArchiveSystem7x.Models.CColumnsModel.Create_UserListColumns();
            ViewBag.Columns = columns;
            var role = SGArchiveSystem7x.Models.CComboboxModel.Create_ComboboxRole(true);
            var dep = SGArchiveSystem7x.Models.CComboboxModel.Create_ComboboxDepartment(true, false);
            ViewBag.Dep = dep;
            ViewBag.Role = role;
            return View();
        }
        /// <summary>
        /// 获取用户列表
        /// </summary>
        /// <param name="name"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Get_ajaxUserList(string rname,int dep,int rolename, int status)
        {
            int count = 0;
            var list = SGArchiveSystem7xDB.Linq.T_UserDal.GetUser(rname,dep,rolename ,out count,status, 0, App_Code.CWebCommons._iPageRows);
            List<string> list1 = new List<string>(4);
            list1.Add(rname);
            list1.Add(dep.ToString());
            list1.Add(rolename.ToString());
            list1.Add(status.ToString());
            App_Code.CSession.Add_QueryWhere(Session, list1);
            var json = new
            {
                rows = list.ToArray(),
                total = count
            };
            return Json(json, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 获取用户列表分页
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Get_ajaxUserListPager(int num, int size)
        {
            ///分页后的起始行
            int iStart = (num - 1) * size;
            var listWhere = App_Code.CSession.Get_QueryWhere(Session);
            int count = 0;
            var list = SGArchiveSystem7xDB.Linq.T_UserDal.GetUser(listWhere[0], int.Parse(listWhere[1]), int.Parse(listWhere[2]),out count,int.Parse(listWhere[3]), iStart, App_Code.CWebCommons._iPageRows);
            var json = new
            {
                rows = list.ToArray(),
                total = count
            };
            return Json(json, JsonRequestBehavior.AllowGet);
        }
        public ActionResult UserInfo()
        {
            ///部门下拉框
            var listDep = SGArchiveSystem7x.Models.CComboboxModel.Create_ComboboxDepartment(true, false);
            var listRole = SGArchiveSystem7x.Models.CComboboxModel.Create_ComboboxRole(true);

            ViewBag.DEP = listDep;
            ViewBag.ROLE = listRole;

            return View();
        }

        //添加User
        [HttpPost]
        public string Get_ajaxUserAdd(string uname, string rname, string pwd, string ip, int ad, int  ISIP, string phone, string email, int role, int dep, int status,int i01)
        {
            var user = SGArchiveSystem7x.App_Code.CSession.Get_User(Session);
            T_User u = new T_User()
            {
                U_NAME = uname,
                U_RNAME = rname,
                U_PWD = SGArchiveSystem7xCommons.PWD_MD5.MD5Encrypt("123456"),
                U_IP = ip,
                U_DEPID = dep,
                U_AD = ad,
                U_ISIP = ISIP,
                U_V04 = phone,
                U_V01 = email,
                U_ROLEID = role,
                U_D01 = DateTime.Now,
                U_STATUS = status,
                ///补代码
                U_LASTDATETIME = DateTime.Now,
                U_LASTUSERNAME = user.U_RNAME,
                U_I01=i01
            };
            var value = (int)T_UserDal.Add_User(u);
            return  value.ToString();
        }


        //修改User
        [HttpPost]
        public string Get_ajaxUserModify(int id, string uname, string rname, string pwd, string ip, int ad, int ISIP, string phone, string email, int role, int dep, int status, int i01)
        {
            var user = SGArchiveSystem7x.App_Code.CSession.Get_User(Session);
            T_User u = new T_User()
            {
                U_ID = id,
                U_NAME = uname,
                U_RNAME = rname,
                //U_PWD = SGArchiveSystem7xCommons.PWD_MD5.MD5Encrypt(pwd),
                U_IP = ip,
                U_AD = ad,
                U_DEPID = dep,
                U_ISIP = ISIP,
                U_V04 = phone,
                U_V01 = email,
                U_ROLEID = role,
                U_STATUS = status,
                ///补代码
                U_LASTDATETIME = DateTime.Now,
                U_LASTUSERNAME = user.U_RNAME,
                U_I01 = i01
            };
            var value = (int)T_UserDal.Modify_User(u);
            return  value.ToString();
        }

        /// <summary>
        /// 获取当个用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Get_ajaxUserId(int id)
        {
            var en = T_UserDal.Get_UserId(id);
            return Json(en, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string  Get_ajaxResetPwd(int id ,string pwd )
        {
         // pwd = SGArchiveSystem7xCommons.PWD_MD5.MD5Encrypt(pwd);
            var value = (int)T_UserDal.Modify_Pwd(id, pwd);
            return value.ToString();
        }
    }
}